package com.elitel.frame.business.controller;

import java.io.File;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elitel.frame.base.component.RedisCacheTreeConfig;
import com.elitel.frame.base.service.JarServerConfigService;
import com.elitel.frame.business.service.BaseJarServerService;
import com.elitel.frame.main.entity.BaseJarinfo;
import com.elitel.frame.main.entity.BaseJarserver;
import com.elitel.frame.main.entity.CfgDb;
import com.elitel.frame.main.entity.vo.JsonDataResult;

/**
 * jar服务接口调用控制
 * 
 * @author dingdaifu
 *
 */
@Controller
@RequestMapping("/business/jarserver")
public class BaseJarServerController {

	@Autowired
	private BaseJarServerService baseJarServerServiceImpl;
	@Resource
	private RedisCacheTreeConfig redisCacheTreeConfig;

	@Resource
	private JarServerConfigService jarServerConfigServiceImpl;

	/**
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/selectData")
	@ResponseBody
	public Object getJarServerData(HttpServletRequest request) {
		JsonDataResult jsonDataResult = new JsonDataResult();
		String serviceID = request.getParameter("serviceID");
		BaseJarserver baseJarserver = jarServerConfigServiceImpl.selectServer(serviceID);
		BaseJarinfo baseJar = jarServerConfigServiceImpl.selectInfo(baseJarserver.getJarName());
		CfgDb cfgDb = redisCacheTreeConfig.selectCfgDbOne(baseJarserver.getDbKey());
		List<Map<String, Object>> twosql = null;
		List<Map<String, Object>> onesql = null;
		// jar服务配置第一个sql执行的结果
		if (baseJarserver.getQueryList() != null && !"".equals(baseJarserver.getQueryList())) {
			onesql = baseJarServerServiceImpl.selectSqlData(cfgDb, baseJarserver.getQueryList());
		}
		// jar服务配置第二个sql执行的结果
		if (baseJarserver.getAttach() != null && !"".equals(baseJarserver.getAttach())) {
			twosql = baseJarServerServiceImpl.selectSqlData(cfgDb, baseJarserver.getAttach());
		}
		File file = new File(baseJar.getJarUrl());// jar包的路径
		String path = baseJar.getJarUrl();
		System.out.println(path);
		path = path.replaceAll("\\\\", "/");
		path = "file:" + path;
		Object o1 = null;
		Object o2 = null;
		try {
			URL url1 = new URL(path);
			URLClassLoader myClassLoader1 = new URLClassLoader(new URL[] { url1 },
					Thread.currentThread().getContextClassLoader());
			String xx = baseJarserver.getJarPackageName() + "." + baseJarserver.getJarClassName();
			Class<?> cls = myClassLoader1.loadClass(xx);
			Object obj = cls.newInstance();
			Method beforMethod = cls.getMethod("beforeMethod", Object.class);// 方法名和对应的各个参数的类型
			Method afterMethod = cls.getMethod("afterMethod", Object.class);// 方法名和对应的各个参数的类型
			//隨便做個筆記，invoke方法執行加載類裏面的方法，第一個參數是必須的，可以為空，但是調用的方法必須是靜態的，非靜態方法必須創建對象
			//上述的newinstance即爲創建對象
			if (obj != null) {
				o1 = beforMethod.invoke(obj, onesql);
				o2 = afterMethod.invoke(obj, twosql);
			}
//			Method method = cls.getMethod(baseJarserver.getJarMethodName(), Object.class, Object.class);// 方法名和对应的各个参数的类型
//
//			o = method.invoke(obj, o1, o2);// 调用得到的上边的方法method(静态方法，第一个参数可以为null)
			//System.out.println(String.valueOf(o));// 输出"000chen000","chen"字符串两边各加3个"0"字符串
		} catch (Exception e) {
			e.printStackTrace();
		}
		return o2;
	}

}
